package com.blockchain.console.service.system.achieve;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.blockchain.console.config.ConsoleConst;
import com.blockchain.console.service.system.BankManage;
import com.blockchain.console.service.system.entity.Bank;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.StringHelper;

public class BankManageImpl extends AbstractSystemService implements
		BankManage {
	public static class BankManageFactory implements
			ServiceFactory<BankManage> {

		@Override
		public BankManage newInstance(ServiceResource serviceResource) {
			return new BankManageImpl(serviceResource);
		}
	}
	
	

	public BankManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}



	@Override
	public void addBank(String name) throws Throwable {
		if(StringHelper.isEmpty(name) || name.length()>30){
			throw new ParameterException("银行名称不能为空且长度不能大于30");
		}
		insert(getConnection(ConsoleConst.DB_USER), "INSERT INTO T6052 SET F02=?", name);
		
	}

	@Override
	public void updateBank(int id,String name) throws Throwable {
		if(id <= 0){
			return;
		}
		if(StringHelper.isEmpty(name) || name.length()>30){
			throw new ParameterException("银行名称不能为空且长度不能大于30");
		}
		execute(getConnection(ConsoleConst.DB_USER), "UPDATE T6052 SET F02 = ? WHERE F01 = ?", name,id);
		
	}

	@Override
	public PagingResult<Bank> searchBank(String name,Paging paging) throws Throwable {
		StringBuilder sql = new StringBuilder("SELECT F01,F02 FROM T6052 WHERE 1=1 ");
		ArrayList<Object> list = new ArrayList<>();
		if( ! StringHelper.isEmpty(name)){
			SQLConnectionProvider provider = getSQLConnectionProvider();
			sql.append(" AND F02 LIKE ?");
			list.add(provider.allMatch(name));
		}
		sql.append(" ORDER BY F01 DESC");
		return selectPaging(getConnection(ConsoleConst.DB_USER), new ArrayParser<Bank>() {

			@Override
			public Bank[] parse(ResultSet rs) throws SQLException {
				ArrayList<Bank> list = null;
				while(rs.next()){
					Bank bank = new Bank();
					bank.id = rs.getInt(1);
					bank.name = rs.getString(2);
					if(list == null){
						list = new ArrayList<>();
					}
					list.add(bank);
				}
				return list == null || list.size() == 0 ? null : list
						.toArray(new Bank[list.size()]);
			}
		}, paging, sql.toString(), list);
	}

	



	@Override
	public Bank get(int id) throws Throwable {
		try(Connection connection = getConnection(ConsoleConst.DB_USER)){
			try(PreparedStatement ps = connection.prepareStatement("SELECT F01,F02 FROM T6052 WHERE F01 = ?")){
				ps.setInt(1, id);
				try(ResultSet rs = ps.executeQuery()){
					Bank bank = null;
					if(rs.next()){
						bank = new Bank();
						bank.id = rs.getInt(1);
						bank.name = rs.getString(2);
					}
					return bank;
				}
			}
		}
	}



	@Override
	public boolean isBankName(String name) throws Throwable {
		try(Connection connection = getConnection(ConsoleConst.DB_USER)){
			try(PreparedStatement ps = connection.prepareStatement("SELECT F01,F02 FROM T6052 WHERE F02 = ?")){
				ps.setString(1, name);
				try(ResultSet rs = ps.executeQuery()){
					if(rs.next()){
						return true;
					}
				}
			}
		}
		return false;
	}
}
